package com.zhongcw.test.spring.boot.swagger2.commodity.controller;

import com.zhongcw.test.spring.boot.swagger2.pojp.Commodity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 商品控制器
 *
 * @author ZCW
 * @createTime 2018/3/27
 * @lastUpdateTime 2018/3/27 ZCW
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/Commoditys")
public class CommodityController {

    private static Map<String, Commodity> commoditys = Collections.synchronizedMap(new HashMap<String, Commodity>());

    static {
        commoditys.put("com1", new Commodity("com1"));
        commoditys.put("com2", new Commodity("com2"));
        commoditys.put("com3", new Commodity("com3"));
        commoditys.put("com4", new Commodity("com4"));
    }

    @ApiOperation("获取所有商品")
    @GetMapping("/")
    public Collection<Commodity> getCommodityList() {
        return commoditys.values();
    }

    @ApiOperation("根据商品名称获取商品")
    @ApiImplicitParam(name = "comName", value = "商品名称", required = true, paramType = "path", defaultValue = "com1")
    @GetMapping("/{comName}")
    public Commodity getCommodity(@PathVariable String comName) {
        return commoditys.get(comName);
    }

    @ApiOperation(value = "添加商品", notes = "商品实体通过Json传输")
//    @ApiImplicitParam(name = "commodity", value = "商品实体", required = true, paramType = "body", dataType = "Commodity")
    @PostMapping("/")
    public boolean addCommodity(@RequestBody Commodity commodity) {
        commoditys.put(commodity.getName(), commodity);
        return true;
    }

    @ApiOperation("添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "comName", value = "商品名称", required = true, paramType = "path"),
//            @ApiImplicitParam(name = "commodity", value = "商品实体", required = true, paramType = "body",dataTypeClass = Commodity.class)
    })
    @PutMapping("/{comName}")
    public boolean updateCommodity(@PathVariable String comName, @RequestBody Commodity commodity) {
        commoditys.put(comName, commodity);
        return true;
    }

    @ApiOperation(value = "删除商品",notes = "根据商品名称删除")
    @ApiImplicitParam(name = "comName",value = "商品名称",required = true,paramType = "path")
    @DeleteMapping("/{comName}")
    public boolean deleteCommodity(@PathVariable String comName) {
        commoditys.remove(comName);
        return true;
    }

}
